import logging
import logging.handlers

def get_logger():
    LOG_FILENAME = "/Users/luofan/PyCharm/ShijiaZhuang/logs/app.log"

    # 获取logger
    logger = logging.getLogger('myapp')

    # 如果logger已经有处理器，则不重复添加
    if not logger.hasHandlers():
        log_handler = logging.handlers.TimedRotatingFileHandler(
            LOG_FILENAME,
            when='H',
            interval=1,
            backupCount=24
        )
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        log_handler.setFormatter(formatter)

        # 添加StreamHandler以便日志输出到控制台
        ch = logging.StreamHandler()
        ch.setFormatter(formatter)

        # 添加处理器
        logger.addHandler(log_handler)
        logger.addHandler(ch)

        # 设置日志级别
        logger.setLevel(logging.INFO)

    return logger
